package com.zx.mes.hyl.controller;

import com.zx.mes.hyl.constant.Constants;
import com.zx.mes.hyl.pagemodel.PageParam;
import com.zx.mes.hyl.strategy.IKeyGenerateStrategy;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * com.zx.mes.hyl.controller.BaseController 扩展
 *
 * @author 华云龙
 * @date 2018-11-29
 */
public class BaseControllerExt {

    @SuppressWarnings("SpringJavaAutowiredMembersInspection")
    @Autowired
    private IKeyGenerateStrategy keyGenerateStrategy;

    /**
     * 获取UUID
     *
     * @return string
     */
    public String getUUID() {
        return keyGenerateStrategy.creator();
    }


    /**
     * 将字符串按指定规则进行分隔,以list返回
     *
     * @param sourceStr 要分隔的字符串
     * @param separator 分隔符,如果分隔符为空,则默认分隔符为","
     * @return java.util.List list
     */
    public List<String> divideStrToList(String sourceStr, String separator) {

        String tempSeparator = separator;

        if (StringUtils.isBlank(sourceStr)) {
            return null;
        }
        if (StringUtils.isBlank(tempSeparator)) {
            tempSeparator = Constants.DEFAULT_DELETE_BATCH_SEPARATOR;
        }

        String[] strArray = sourceStr.split(tempSeparator);
        return Arrays.asList(strArray);
    }

    public List<String> divideStrToList(String sourceStr) {
        return divideStrToList(sourceStr,"");
    }

    /**
     * 将获取分页的信息代码封装
     * @param params java.util.Map params
     * @return com.zx.mes.hyl.pagemodel.Page  page
     */
    public PageParam getPageParams(Map<String,Object> params){
        int page = Integer.valueOf(params.get(Constants.MYBATIS_PAGE).toString());
        int limit = Integer.valueOf(params.get(Constants.MYBATIS_LIMIT).toString());
        PageParam p = new PageParam();
        p.setPage(page).setLimit(limit);
        return p;
    }
}
